unit MainRDM;

{$WARN SYMBOL_PLATFORM OFF}

interface

uses
  Windows, Messages, SysUtils, Classes, ComServ, ComObj, VCLCom, DataBkr,
  DBClient, PoslovanjeSrv_TLB, StdVcl, DB, SqlExpr, FMTBcd, Variants,
  Session, BaseRDM, WideStrings, DBXFirebird;

type
  EFillClassHierarchyListError = class(EPoslovanjeError);

  TrdmMain = class(TRemoteDataModule, IrdmMain)
    mntMain: TSQLMonitor;
    sqlcnMain: TSQLConnection;
  private
    FClassHierarchyList: TStringList;
    FReadOnly: Boolean;
    procedure FillClassHierarchyList(AList: TStringList);
    function GetBaseRDM(const COMObjectFactory: TCOMObjectFactory): IrdmBase;
  protected
    class procedure UpdateRegistry(Register: Boolean; const ClassID,
      ProgID: string); override;
    function Get_Artikel: IrdmBase; safecall;
    function Get_DDV: IrdmBase; safecall;
    function Get_Delavec: IrdmBase; safecall;
    function Get_Dokument: IrdmBase; safecall;
    function Get_Drzava: IrdmBase; safecall;
    function Get_EnotaMere: IrdmBase; safecall;
    function Get_GlavnaKnjiga: IrdmBase; safecall;
    function Get_GlavnaKnjigaReport: IrdmBase; safecall;
    function Get_KnjigaDolgovReport: IrdmBase; safecall;
    function Get_KnjigaIzdanihRacunov: IrdmBase; safecall;
    function Get_KnjigaIzdanihRacunovReport: IrdmBase; safecall;
    function Get_KnjigaPrejetihRacunov: IrdmBase; safecall;
    function Get_KnjigaPrejetihRacunovReport: IrdmBase; safecall;
    function Get_KnjigaTerjatevReport: IrdmBase; safecall;
    function Get_Konto: IrdmBase; safecall;
    function Get_KontoSaldo: IrdmBase; safecall;
    function Get_Objekt: IrdmBase; safecall;
    function Get_OsnovnoSredstvo: IrdmBase; safecall;
    function Get_Podjetje: IrdmBase; safecall;
    function Get_PodrazredList(RazredID: Integer): WideString; safecall;
    function Get_PoslovniPartner: IrdmBase; safecall;
    function Get_PoslovnoLeto: IrdmBase; safecall;
    function Get_Posta: IrdmBase; safecall;
    function Get_PravniStatus: IrdmBase; safecall;
    function Get_Razred: IrdmBase; safecall;
    function Get_Report: IrdmBase; safecall;
    function Get_Sifrant: IrdmBase; safecall;
    function Get_Skupina: IrdmBase; safecall;
    function Get_SkupinaArtikel: IrdmBase; safecall;
    function Get_SkupinaPoslovniPartner: IrdmBase; safecall;
    function Get_SkupinaTemeljnica: IrdmBase; safecall;
    function Get_StroskiDela: IrdmBase; safecall;
    function Get_Temeljnica: IrdmBase; safecall;
    function Get_TemeljnicaReport: IrdmBase; safecall;
    function Get_Test: IrdmBase; safecall;
    function Get_Valuta: IrdmBase; safecall;
    procedure Prijava(const Username, Password: WideString; Params: OleVariant;
      var Session: ISession); safecall;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  end;

resourcestring
  FillClassHierarchyListError = 'Napaka pri izdelavi liste razredov';

implementation

{$R *.DFM}

uses
  RazredRDM,
  SkupinaRDM, SkupinaArtikelRDM, SkupinaPoslovniPartnerRDM, SkupinaTemeljnicaRDM,
  ObjektRDM,
  SifrantRDM, ArtikelRDM, DDVRDM, DrzavaRDM, EnotaMereRDM, DelavecRDM,
  KontoRDM, PodjetjeRDM, PoslovniPartnerRDM, PoslovnoLetoRDM, PostaRDM,
  PravniStatusRDM, ValutaRDM, KontoSaldoRDM, StroskiDelaRDM, OsnovnoSredstvoRDM,
  DokumentRDM, TemeljnicaRDM, TemeljnicaReportRDM,
  GlavnaKnjigaRDM, GlavnaKnjigaReportRDM,
  KnjigaIzdanihRacunovRDM, KnjigaIzdanihRacunovReportRDM,
  KnjigaPrejetihRacunovRDM, KnjigaPrejetihRacunovReportRDM,
  KnjigaTerjatevReportRDM, KnjigaDolgovReportRDM,
  ReportRDM, TestRDM;

constructor TrdmMain.Create(AOwner: TComponent);
begin
  inherited;
  ReportMemoryLeaksOnShutdown := True;
  sqlcnMain.LoadParamsFromIniFile('Poslovanje.ini');
  FClassHierarchyList := TStringList.Create;
  FillClassHierarchyList(FClassHierarchyList);
end;

destructor TrdmMain.Destroy;
begin
  FClassHierarchyList.Free;
  inherited;
end;

procedure TrdmMain.FillClassHierarchyList(AList: TStringList);
var
  SQLDataSet: TSQLDataSet;
  ID: Integer;
  ClassSuccesors: string;

  function GetClassSuccessors(AID: Integer): string;
  var
    ASQLDataSet: TSQLDataSet;
    HasValues: Boolean;
    PODID: Integer;
  begin
    Result := '';
    HasValues := False;
    ASQLDataSet := TSQLDataSet.Create(Self);
    try
      ASQLDataSet.SQLConnection := sqlcnMain;
      ASQLDataSet.CommandText := Format('select ID from RAZ_GET_PODRAZRED_ID(%d, 0)', [AID]);
      try
        ASQLDataSet.Open;
        while not ASQLDataSet.Eof do
        begin
          PODID := ASQLDataSet.Fields[0].AsInteger;
          if HasValues then
            Result := Format('%s, %d', [Result, PODID])
          else
            Result := Format('%d', [PODID]);
          HasValues := True;
          ASQLDataSet.Next;
        end;
      except
        raise EFillClassHierarchyListError.CreateFmt(FillClassHierarchyListError, []);
      end;
    finally
      ASQLDataSet.Free;
    end;
  end;

begin
  AList.Clear;
  SQLDataSet := TSQLDataSet.Create(Self);
  try
    SQLDataSet.SQLConnection := sqlcnMain;
    SQLDataSet.CommandText := 'select ID from RAZRED';
    try
      SQLDataSet.Open;
      while not SQLDataSet.Eof do
      begin
        ID := SQLDataSet.Fields[0].AsInteger;
        ClassSuccesors := GetClassSuccessors(ID);
        if ClassSuccesors <> '' then
          AList.Add(Format('%d=%s',[ID, ClassSuccesors]));
        SQLDataSet.Next;
      end;
    except
      raise EFillClassHierarchyListError.CreateFmt(FillClassHierarchyListError, []);
    end;
  finally
    SQLDataSet.Free;
  end;
end;

function TrdmMain.GetBaseRDM(const COMObjectFactory: TCOMObjectFactory): IrdmBase;
begin
  Result := COMObjectFactory.CreateCOMObject(nil) as IrdmBase;
  Result.SQLConnection := Integer(sqlcnMain);
  Result.ReadOnly := FReadOnly;
end;

function TrdmMain.Get_Artikel: IrdmBase;
begin
  Result := GetBaseRDM(ArtikelFactory);
end;

function TrdmMain.Get_DDV: IrdmBase;
begin
  Result := GetBaseRDM(DDVFactory);
end;

function TrdmMain.Get_Delavec: IrdmBase;
begin
  Result := GetBaseRDM(DelavecFactory);
end;

function TrdmMain.Get_Dokument: IrdmBase;
begin
  Result := GetBaseRDM(DokumentFactory);
end;

function TrdmMain.Get_Drzava: IrdmBase;
begin
  Result := GetBaseRDM(DrzavaFactory);
end;

function TrdmMain.Get_EnotaMere: IrdmBase;
begin
  Result := GetBaseRDM(EnotaMereFactory);
end;

function TrdmMain.Get_GlavnaKnjiga: IrdmBase;
begin
  Result := GetBaseRDM(GlavnaKnjigaFactory);
end;

function TrdmMain.Get_GlavnaKnjigaReport: IrdmBase;
begin
  Result := GetBaseRDM(GlavnaKnjigaReportFactory);
end;

function TrdmMain.Get_KnjigaDolgovReport: IrdmBase;
begin
  Result := GetBaseRDM(KnjigaDolgovReportFactory);
end;

function TrdmMain.Get_KnjigaIzdanihRacunov: IrdmBase;
begin
  Result := GetBaseRDM(KnjigaIzdanihRacunovFactory);
end;

function TrdmMain.Get_KnjigaIzdanihRacunovReport: IrdmBase;
begin
  Result := GetBaseRDM(KnjigaIzdanihRacunovReportFactory);
end;

function TrdmMain.Get_KnjigaPrejetihRacunov: IrdmBase;
begin
  Result := GetBaseRDM(KnjigaPrejetihRacunovFactory);
end;

function TrdmMain.Get_KnjigaPrejetihRacunovReport: IrdmBase;
begin
  Result := GetBaseRDM(KnjigaPrejetihRacunovReportFactory);
end;

function TrdmMain.Get_KnjigaTerjatevReport: IrdmBase;
begin
  Result := GetBaseRDM(KnjigaTerjatevReportFactory);
end;

function TrdmMain.Get_Konto: IrdmBase;
begin
  Result := GetBaseRDM(KontoFactory);
end;

function TrdmMain.Get_KontoSaldo: IrdmBase;
begin
  Result := GetBaseRDM(KontoSaldoFactory);
end;

function TrdmMain.Get_Objekt: IrdmBase;
begin
  Result := GetBaseRDM(ObjektFactory);
end;

function TrdmMain.Get_OsnovnoSredstvo: IrdmBase;
begin
  Result := GetBaseRDM(OsnovnoSredstvoFactory);
end;

function TrdmMain.Get_Podjetje: IrdmBase;
begin
  Result := GetBaseRDM(PodjetjeFactory);
end;

function TrdmMain.Get_PodrazredList(RazredID: Integer): WideString;
begin
  Result := FClassHierarchyList.Values[IntToStr(RazredID)];
  if Result = '' then
    Result := '-1';
end;

function TrdmMain.Get_PoslovnoLeto: IrdmBase;
begin
  Result := GetBaseRDM(PoslovnoLetoFactory);
end;

function TrdmMain.Get_PoslovniPartner: IrdmBase;
begin
  Result := GetBaseRDM(PoslovniPartnerFactory);
end;

function TrdmMain.Get_Posta: IrdmBase;
begin
  Result := GetBaseRDM(PostaFactory);
end;

function TrdmMain.Get_PravniStatus: IrdmBase;
begin
  Result := GetBaseRDM(PravniStatusFactory);
end;

function TrdmMain.Get_Razred: IrdmBase;
begin
  Result := GetBaseRDM(RazredFactory);
end;

function TrdmMain.Get_Report: IrdmBase;
begin
  Result := GetBaseRDM(ReportFactory);
end;

function TrdmMain.Get_Sifrant: IrdmBase;
begin
  Result := GetBaseRDM(SifrantFactory);
end;

function TrdmMain.Get_Skupina: IrdmBase;
begin
  Result := GetBaseRDM(SkupinaFactory);
end;

function TrdmMain.Get_SkupinaArtikel: IrdmBase;
begin
  Result := GetBaseRDM(SkupinaArtikelFactory);
end;

function TrdmMain.Get_SkupinaPoslovniPartner: IrdmBase;
begin
  Result := GetBaseRDM(SkupinaPoslovniPartnerFactory);
end;

function TrdmMain.Get_SkupinaTemeljnica: IrdmBase;
begin
  Result := GetBaseRDM(SkupinaTemeljnicaFactory);
end;

function TrdmMain.Get_StroskiDela: IrdmBase;
begin
  Result := GetBaseRDM(StroskiDelaFactory);
end;

function TrdmMain.Get_Temeljnica: IrdmBase;
begin
  Result := GetBaseRDM(TemeljnicaFactory);
end;

function TrdmMain.Get_TemeljnicaReport: IrdmBase;
begin
  Result := GetBaseRDM(TemeljnicaReportFactory);
end;

function TrdmMain.Get_Test: IrdmBase;
begin
  Result := GetBaseRDM(TestFactory);
end;

function TrdmMain.Get_Valuta: IrdmBase;
begin
  Result := GetBaseRDM(ValutaFactory);
end;

procedure TrdmMain.Prijava(const Username, Password: WideString;
  Params: OleVariant; var Session: ISession);
begin
  if (Username <> '') and (Password <> '') then
  begin
    Session := SessionFactory.CreateComObject(nil) as ISession;
    Session.Username := Username;
    Session.Data := Params;
    FReadOnly := Session.Params['READ_ONLY'];
  end;
  if VarIsNull(Session.Params['PODJETJE_ID']) or
    VarIsEmpty(Session.Params['PODJETJE_ID']) or
    VarIsNull(Session.Params['POSLOVNO_LETO_ID']) or
    VarIsEmpty(Session.Params['POSLOVNO_LETO_ID']) then
  begin
    Session := nil;
  end;
end;

class procedure TrdmMain.UpdateRegistry(Register: Boolean; const ClassID,
  ProgID: string);
begin
  if Register then
  begin
    inherited UpdateRegistry(Register, ClassID, ProgID);
    EnableSocketTransport(ClassID);
    EnableWebTransport(ClassID);
  end else
  begin
    DisableSocketTransport(ClassID);
    DisableWebTransport(ClassID);
    inherited UpdateRegistry(Register, ClassID, ProgID);
  end;
end;

initialization
  TComponentFactory.Create(ComServer, TrdmMain,
    Class_rdmMain, ciMultiInstance, tmApartment);
end.
